<?php

class BlockAction extends PublicAction
{
    //列表页
    public function index()
    {
        $block_db = M('Block');

        $map['lang'] = array('eq', LANG_ID);

        $this->assign($_REQUEST);

        //取得满足条件的记录总数
        $count = $block_db->where($map)->count('id');

        if ($count > 0) {
            import("@.ORG.Page");

            $listRows = I('listRows', 15, 'intval');

            //创建分页对象
            $p = new Page($count, $listRows);

            //分页查询数据
            $voList = $block_db->where($map)->limit($p->firstRow . ',' . $p->listRows)->select();

            //分页显示
            $page = $p->show();

            //模板赋值显示
            $this->assign('list', $voList);
            $this->assign('page', $page);
        }

        //记录当前位置
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->display();
    }

    //编辑
    public function edit()
    {
        $block_db = M('Block');
        if (IS_POST) {

            if (false === $block_db->create()) {
                $this->error($block_db->getError());
            }

            if (false !== $block_db->save()) {
                $this->success('修改成功!', cookie('__forward__'));
            } else {
                $this->success('修改失败!: ' . $block_db->getDbError());
            }
        } else {
            $id = $_GET['id'];
            $vo = $block_db->getById($id);

            $this->assign('vo', $vo);
            $this->display();
        }
    }

    function add()
    {
        if (IS_POST) {
            $block_db = D('Block');

            $_POST['lang'] = LANG_ID;
            if (false === $block_db->create()) {
                $this->error($block_db->getError());
            }

            $id = $block_db->add();

            if ($id !== false) {
                $this->success('添加成功！', cookie('__forward__'));
            } else {
                $this->error('提交失败！: ' . $block_db->getDbError());
            }
        } else {
            $this->display();
        }
    }

    function delete()
    {
        $block_db = M('Block');
        $id = $_GET['id'];

        if (isset($id)) {

            if (false !== $block_db->delete($id)) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错: ' . $block_db->getDbError());
            }
        } else {
            $this->error('缺少参数');
        }
    }
}